
#include <string.h>
#include <stdio.h>
#include <malloc.h>
#include "tcpclient.h"
#include "http2hj212.h"
#include "iniparser.h"
#include "log.h"


static void _free_yapicf_content(YapiSerConfig* config)
{
    free(config->ip);
    free(config->port);
    free(config->index);
    free(config->space);
}

static void _free_hj212cf_content(Hj212SerConfig* config)
{
    free(config->ip);
    free(config->pwd);
    free(config->mn);
}

static int _init_config(dictionary* ini, YapiSerConfig* apiconfig, Hj212SerConfig* hjconfig)
{
    int ret = !!ini && !!apiconfig && !!hjconfig;

    if ( ret )
    {
        apiconfig->ip = strdup(iniparser_getstring(ini, "DEVICE:ip",  "127.0.0.1"));
        apiconfig->port = strdup(iniparser_getstring(ini, "DEVICE:port",  "9088"));
        apiconfig->index = strdup(iniparser_getstring(ini, "DEVICE:index", " "));
        apiconfig->space = strdup(iniparser_getstring(ini, "DEVICE:space", "space"));
        printf("port: %s\n", apiconfig->port);
        if ( !apiconfig->ip || !apiconfig->port || !apiconfig->index || !apiconfig->space )
        {
            _free_yapicf_content(apiconfig);
            ret = 0;
        }

        hjconfig->ip = strdup(iniparser_getstring(ini, "SERVER:ip", "127.0.0.1"));
        hjconfig->port = iniparser_getint(ini, "SERVER:port", 9016);
        hjconfig->pwd = strdup(iniparser_getstring(ini, "SERVER:pwd", " "));
        hjconfig->mn = strdup(iniparser_getstring(ini, "SERVER:mn", "123234234324"));

        if (!hjconfig->ip || !hjconfig->pwd || !hjconfig->mn )
        {
           
            _free_hj212cf_content(hjconfig);
            _free_yapicf_content(apiconfig);
            ret = 0;
        }
    }
    
    return ret;
}

static void _free_hthjdev(HtHJDev* dev)
{
    free(dev);
    free(dev->hj212serconf);
    free(dev->yapiserconf);
    dev = NULL;
}

HtHJDev* hthj_ctreat(const char* inifile)
{
    HtHJDev* HtHJ_Dev = (HtHJDev*)malloc(sizeof(HtHJDev));
    
    if ( HtHJ_Dev )
    {
        HtHJ_Dev->yapiserconf = (YapiSerConfig*)malloc(sizeof(YapiSerConfig));
        HtHJ_Dev->hj212serconf = (Hj212SerConfig*)malloc(sizeof(Hj212SerConfig));

        if ( HtHJ_Dev->hj212serconf && HtHJ_Dev->yapiserconf )
        {
            dictionary* ini = iniparser_load(inifile);
           
            if ( ini )
            {
                if ( _init_config(ini, HtHJ_Dev->yapiserconf, HtHJ_Dev->hj212serconf) )  // 正确读取配置文件
                {
                    
                }
                else
                {
                    LOG_E("read ini file failed\n");
                    free(HtHJ_Dev);
                }

                iniparser_freedict(ini);
            }
            else
            {
                LOG_E("iniparser_load failed\n");
                _free_hthjdev(HtHJ_Dev);
            }
        }
        else
        {
            LOG_E("not enough space to malloc HtHJDev...\n");
            _free_hthjdev(HtHJ_Dev);
        }
    }

    return HtHJ_Dev;
}

void register_csv_parser(HtHJDev* dev, CsvParser* parser)
{
    if ( dev ) dev->csvparser = parser;
}
void register_yapi_parser(HtHJDev* dev, YapiParser* parser)
{
    if ( dev ) dev->yapiparser = parser;
}

void register_hj212_dev(HtHJDev* dev, HJ212PackFact* hj212dev)
{
    if ( dev ) dev->hj212dev = hj212dev;
}

void register_envpro_dev(HtHJDev* dev, EnvProDev* envprodev)
{
    if ( dev ) dev->envprodev = envprodev;
}

void register_tcp_client(HtHJDev* dev, TcpClient* client)
{
    if ( dev ) dev->tcpclient = client;
}

HtHJDev* hthj_destroy(HtHJDev* hjdev)
{
    if (hjdev)
    {
        if (hjdev->envprodev) envprodev_destroy(hjdev->envprodev);
        if (hjdev->hj212dev) hj212_pack_fact_destroy(hjdev->hj212dev);
        if (hjdev->yapiparser) yapiparser_destroy(hjdev->yapiparser);
        if (hjdev->csvparser) csvparser_free(hjdev->csvparser);
    }
}